<script type="text/javascript"><!--//--><![CDATA[//><!--

	var grid_reload = function(){
		$('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
	};

	$(document).ready(function() {
		// Setup Datagrid
		$('#datagrid').datagrid({  
			url:gs_path+'/'+controller+'/getdata', 
			queryParams: $('#fm_search').serializeObject(),
			height:340,
			pagination:true,
			columns:[[
				{ field : 'ck', checkbox:true, width: 40, align: 'center' },				
				{ field : 'barang_nama', title : 'Nama Barang', width:200, sortable:true },
				{ field : 'pd_awal', title : 'Mulai', width:100, sortable:true },
				{ field : 'pd_akhir', title : 'Berakhir', width:100, sortable:true },
				{ field : 'pd_persen', title : 'Persen', width:100, sortable:true },
				{ field : 'pd_diskon', title : 'Diskon', width:100, sortable:true },
				{ field : 'barang_harga', title : 'Harga Jual', width:100, sortable:true },
				{ field : 'pd_harga', title : 'Harga Jual Setelah Diskon', width:200, sortable:true },
				{ field : 'pd_status', title : 'Status Aktif', width:100, sortable:true }
			]]
		});
		
		// Setup Form
		$('#form_master').setupForm([
			{ name: 'barang_nama', type: 'lov', size: 'long',
				onClearButton:function(){
					$(this).setval('');
					$('#barang_harga').setval(''); 
				},
				onActionButton:function(){ 
					LOVDiskon({ onSelect:function(row){ 
						$('#barang_nama').setval(row.barang_nama); 
						$('#barang_harga').setval(row.barang_hjual1);
					} }); 
				}
			},
			{ name: 'barang_harga', type: 'text', size: 'medium', maxlength: 100 },
			{ name: 'pd_awal', type: 'date', size: 'short', maxlength: 100 },
			{ name: 'pd_awal_jam', type: 'text', size: 'short', maxlength: 100 },
			{ name: 'pd_awal_menit', type: 'text', size: 'short', maxlength: 100 },
			{ name: 'pd_akhir', type: 'date', size: 'short', maxlength: 100 },
			{ name: 'pd_akhir_jam', type: 'text', size: 'short', maxlength: 100 },
			{ name: 'pd_akhir_menit', type: 'text', size: 'short', maxlength: 100 },
			{ name: 'pd_persen', type: 'text', size: 'medium', maxlength: 100 },
			{ name: 'pd_diskon', type: 'text', size: 'medium', maxlength: 100 },
			{ name: 'pd_harga', type: 'text', size: 'medium', maxlength: 100 },
			{ name: 'pd_status', type: 'text', size: 'medium', maxlength: 100 }
		]);
		
		// Set Hidden Object

		$('#pd_persen').keyup(function() {
			var harga = $('#barang_harga').val();
			var persen = $('#pd_persen').val(); 
			var subtotal = harga * persen / 100;
			var total = harga - subtotal
  			$('#pd_diskon').setval(subtotal);
  			$('#pd_harga').setval(total);
		});
		
		// Create Dialog
		$('#dialog_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:700,
			height:450,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					$('#form_master').submitForm('/'+controller+'/ajax_post', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); }} );
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dialog_container').dialog('close');
				}
			}]
		});
		
		
		// Set Action Button
		var buttons = [ { label: 'Add', icon: 'add', onclick: function() {
							resetError();
							$('#dialog_container').dialog('open');
							$('#form_master').data('mode', 'create');
							$('#form_master').parent().parent().parent().prev().find('.panel-title').html('New Produk Diskon');
							//$('#kd_vendor').readonly(false);
							$('#barang_nama, #barang_harga, #pd_persen, #pd_diskon, #pd_harga, #pd_status, #pd_akhir, #pd_akhir_jam, #pd_akhir_menit, #pd_awal, #pd_awal_menit, #pd_awal_jam').val('');
							$('#barang_nama').focus();
						} },
						{ label: 'Edit', icon: 'edit', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{ 
								resetError();
								$('#dialog_container').dialog('open'); 
								$('#form_master').data('mode', 'edit');
								$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Edit Produk Diskon');
								$('#pd_id').readonly();
								jsonToForm(oSel); $('#barang_nama').focus();
							} 
						} },
						{label: 'Delete', icon: 'delete', onclick: function() { 
							var oSel = $('#datagrid').datagrid('getSelected'); 
							if (oSel == null) { 
								$.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
							} else { 
								deleteData(['pd_id']);
							} 
						}}];
		setAction(buttons);
		
		
		$('#fm_search').focusFirst();
	});
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div>
        <form id="fm_search" onsubmit="grid_reload(); return false;">
        	<table class="tb_form" style="margin-bottom:0px">
            <tr>
            	<td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                	<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>


<div id="dialog_container" style="padding:5px;width:600px;height:250px;">
	<form id="form_master">
    <table>
    <tr>
        <td class="label" style="width:150px">Nama Barang</td>
        <td><input id="barang_nama" /></td>
    </tr>
</table>
<table>
    <tr>
        <td class="label" style="width:150px">Harga Jual</td>
        <td><input id="barang_harga" /></td>
    </tr>
</table>
<table>    
      <tr>
        <td class="label" style="width:150px">Persen</td>
        <td><input id="pd_persen" /></td>
    </tr>
    </table>
<table>
    <tr>
        <td class="label" style="width:150px">Diskon</td>
        <td><input id="pd_diskon" /></td>
    </tr>
    </table>
<table> 
       <tr>
        <td class="label" style="width:150px">Harga Setelah Diskon</td>
        <td><input id="pd_harga" /></td>
    </tr>
    </table>
<table>
    <tr>
        <td class="label" style="width:150px">Mulai</td>
        <td><input id="pd_awal" /></td>
        <td>Pukul</td>
        <td><input id="pd_awal_jam" /></td>
      	<td>:</td>
        <td><input id="pd_awal_menit" /></td>
    </tr>
    </table>
<table> 
       <tr>
        <td class="label" style="width:150px">Berakhir</td>
        <td><input id="pd_akhir" /></td>
        <td>Pukul</td>
        <td><input id="pd_akhir_jam" /></td>
        <td>:</td>
        <td><input id="pd_akhir_menit" /></td>
    </tr>
    </table>
<table> 
       <tr>
        <td class="label" style="width:150px">Status Aktif</td>
        <td><select id="pd_status" name="pd_status">
        		<option value="Aktif" name="aktif">Aktif</option>
        		<option value="Tidak Aktif" name="tidak aktif">Tidak Aktif</option>
        	 </select></td>
    </tr> 
    </table>
	</form>
</div>